package main

import (
	"gitee.com/gopher2011/godemo/cmd"
	"gitee.com/gopher2011/godemo/config"
	"gitee.com/gopher2011/godemo/global"
	"gitee.com/gopher2011/godemo/logger"
	"gitee.com/gopher2011/godemo/server/dao"
	"github.com/polaris1119/keyword"
	"io/ioutil"
	"os"
	"path/filepath"
	"strconv"
)

func main() {
	if len(os.Args) >= 2 {
		switch os.Args[1] {
		case "indexer":
			cmd.Index()
			return
		case "crawler":
			cmd.Crawl()
			return
		}
	}

	// 根据参数打印版本信息
	global.PrintVersion(os.Stdout)
	savePid()

	global.App.Init(dao.Website().Domain)

	logger.Init(config.Root+"/log", config.Config.MustValue("global", "log_level", "DEBUG"))

	go keyword.Extractor.Init(keyword.DefaultProps, true, config.Root+"/data/programming.txt,"+config.Root+"/data/dictionary.txt")

	go 

}

// 读取配置文件中的主机配置
func getAddr()string{
	host := config.Config.MustValue("listen", "host", "")
	if host == "" {
		global.App.Host = "localhost"
	}else {
		global.App.Host = host
	}
	global.App.Port = config.Config.MustValue("listen", "port", "8088")
	return host + ":" + global.App.Port
}

func savePid(){
	pidFileName := config.Root + "/pid/" + filepath.Base(os.Args[0]) + ".pid"
	pid := os.Getpid()
	ioutil.WriteFile(pidFileName, []byte(strconv.Itoa(pid)), 0755)
}